<?php 
include 'checklogin.php';
if(!empty($_POST)){
    if(empty($_POST['topicId'])){
        exit('提交错误');
    }
    if(empty($_FILES)){
        header("refresh:1;url=upload.php?voteId=".$_POST['topicId']);
        exit('请上传图片');
    }
    //连接数据库
    $conn = mysql_connect('localhost','root','');
    if(!$conn){
        die('Could not connect: ' . mysql_error());
    }else{
        mysql_select_db('test',$conn) or die('Could not select database.');
        mysql_query("set names 'utf8'",$conn);
    }
    //获取该投票的类型
    $votesql = mysql_query("select * from vote_profile where id={$_POST['topicId']}");
    $voteinfo = mysql_fetch_array($votesql,MYSQL_ASSOC);
    $row = $voteinfo;
    $voteId = $_POST['topicId'];
    $infosql = mysql_query("select count(id) As total from vote_infos where topic_id={$_POST['topicId']}");
    $countRes = mysql_fetch_assoc($infosql,MYSQL_ASSOC);
    if($countRes['total']>=2 && $voteinfo['type']==1){
        header("Content-Type: text/html; charset=utf-8");
        header("refresh:1;url=upload.php?voteId=".$_POST['topicId']);
        exit('该类型投票只能两两相pk');
    }
    $dir = '';
    $imgNewname = rand(99999999,6);echo $_FILES['img']['type'];
    switch($_FILES['img']['type'])   
    {
        case "image/pjpeg":
            $fileextname     =     ".jpg";     
            break;
        case "image/jpeg":
            $fileextname     =     ".jpg";     
            break;
        case "image/gif":
            $fileextname     =     ".gif";     
            break;
        case "image/x-png":
            $fileextname     =     ".png"; 
            break;
        case "image/png":
            $fileextname     =     ".png"; 
            break;
        default :
            $fileextname = '';break;
    }
    //处理上传的文件
    if (file_exists("/upload/" . $_FILES["img"]["name"])){
        echo $_FILES["img"]["name"] . " already exists. ";
    }else{
      move_uploaded_file($_FILES["img"]["tmp_name"],
      "upload/" . $imgNewname.$fileextname);
      // echo "Stored in: " . "upload/" . $imgNewname;
    }
    $imgUrl = 'http://img.letian.com/'.$imgNewname.$fileextname;
    //插入数据到数据表
    $data['topic_id'] = $_POST['topicId'];
    $data['create_id'] = $_SESSION['uid'];
    $data['img_url'] = $imgUrl;
    $data['create_time'] = date('Y-m-d H:i:s');
    $save = mysql_query("INSERT INTO vote_infos(`topic_id`,`create_id`,`img_url`,`create_time`)
        values({$data['topic_id']},{$data['create_id']},'{$data['img_url']}','{$data['create_time']}')");
    if(!$save){
        exit('上传失败');
    }

    header("refresh:0;url=voteinfo.php?id=".$_POST['topicId']);
}else{
    if(empty($_GET['voteId'])){
        header("Content-Type: text/html; charset=utf-8");
        header("refresh:0;url=votelist.php");
        exit;
    }
    $voteId = $_GET['voteId'];
    //连接数据库
    $link = mysql_connect('localhost', 'root', '');
    if (!$link) {
        die('Could not connect: ' . mysql_error());
    }else{
        mysql_select_db('test', $link) or die('Could not select database.');
        mysql_query("set names 'utf8'",$link);
    }
    $info = mysql_query("select * from vote_profile where id = {$_GET['voteId']}");
    $row = mysql_fetch_array($info,MYSQL_ASSOC);
}

// print_r($row);exit;

include 'upload.html';

function pr($data){
    print('<pre>');
    print_r($data);
    print('</pre>');
}
?>